package com.zf.shuashua.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.meis.base.mei.base.BaseDialog;
import com.zf.shuashua.R;
import com.zf.shuashua.advert.InteractionAdListener;
import com.zf.shuashua.advert.NativeAdvertManager;

public class GuideAutoPlayDialog extends BaseDialog {

    Activity activity;

    FrameLayout advertContainer;

    OnClickListener clickListener;

    public interface OnClickListener {
        void onClick(BaseDialog dialog);
    }

    public GuideAutoPlayDialog(Activity activity, OnClickListener listener) {
        this.activity = activity;
        this.clickListener = listener;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_guide_auto_play;
    }

    @Override
    protected void initView() {
        advertContainer = getView().findViewById(R.id.advert_container);
        getView().findViewById(R.id.btTitle).setOnClickListener(v -> {
            clickListener.onClick(this);
        });
        setCancelable(false);
    }

    @Override
    protected void initData() {
        getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        advertContainer.postDelayed(() -> NativeAdvertManager.getInstance().loadExpressAd(activity, new InteractionAdListener() {
            @Override
            public void onRenderSuccess(View view) {
                addAdvertView(view);
            }

            @Override
            public void onError() {
            }
        }), 200);
    }

    public void addAdvertView(View view) {
        advertContainer.removeAllViews();
        advertContainer.addView(view);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

}
